﻿
Imports System.Net
Imports System.IO


''' <summary>
''' get stock data from internet interface
''' </summary>
''' <remarks></remarks>
Public Class clsWebInterface

#Region "property"
    ''' <summary>
    ''' stock ID like "600123"
    ''' </summary>    
    ''' <remarks></remarks>
    Public Property StockID As String
        Get
            Return m_StockID
        End Get
        Set(value As String)
            SetUrl(value)
        End Set
    End Property

    Public ReadOnly Property StockInfo As clsStockInfo
        Get
            Return m_StockInfo
        End Get
    End Property


#End Region

#Region "method"
    Public Function GetStockData() As Boolean
        Try
            ' Create a request using a URL that can receive a post. 
            Dim request As HttpWebRequest = WebRequest.Create(m_InterfaceUrl)
            ' Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded"

            ' Get the response.
            Dim response As WebResponse = request.GetResponse()

            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            Dim encode As System.Text.Encoding = System.Text.Encoding.GetEncoding("GB2312")

            ' Open the stream using a StreamReader for easy access.
            Dim reader As StreamReader = New StreamReader(dataStream, encode)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()

            If AnalysisResponseString(responseFromServer) = False Then
                ' Clean up the streams.
                reader.Close()
                dataStream.Close()
                response.Close()
                Return False
            End If

            ' Clean up the streams.
            reader.Close()
            dataStream.Close()
            response.Close()

        Catch ex As Exception
            Return False
        End Try

        Return True
    End Function


#End Region



    Private m_StockID As String 'stock ID,for example "600123"
    Private Const InterfaceUrlPrefix As String = "http://hq.sinajs.cn/list=sh" 'sina stock data interface
    Private m_InterfaceUrl As String 'actual url to be sent
    Private m_StockInfo As clsStockInfo

    Sub New(StockID As String)
        SetUrl(StockID)
        m_StockInfo = New clsStockInfo
    End Sub

    ''' <summary>
    ''' set url from stockID
    ''' </summary>
    ''' <param name="StockID">format="600123"</param>
    ''' <remarks></remarks>
    Private Sub SetUrl(StockID As String)
        m_StockID = StockID
        m_InterfaceUrl = InterfaceUrlPrefix & m_StockID
    End Sub

    ''' <summary>
    ''' analyze response string and fill data into stock data structure
    ''' </summary>
    ''' <param name="ResponseString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function AnalysisResponseString(ResponseString As String) As Boolean
        If ResponseString = String.Empty Then Return False

        Dim strTmp = ResponseString.Substring(ResponseString.IndexOf(Chr(34)) + 1) 'remove chars before "
        strTmp = strTmp.Substring(0, strTmp.Length - 3) 'remove chars after "
        Dim strFields() As String
        strFields = strTmp.Split(",")

        'fields count is not correct means the data maybe wrong
        If strFields.Length <> 33 Then
            Return False
        End If

        'fill structure
        With m_StockInfo
            .StockID = m_StockID
            .StockName = strFields(0)
            .TodayOpen = strFields(1)
            .YestodayClose = strFields(2)
            .CurrentPrice = strFields(3)
            .TodayMax = strFields(4)
            .TodayMin = strFields(5)
            .RefreshTime = CDate(strFields(30) & " " & strFields(31))
        End With
        Return True
    End Function
End Class
